package com.cmc.cloud.cmclink.doc.api.document;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cmc.cloud.cmclink.doc.api.document.rep.BaseCodeRpcRepDto;
import com.cmc.cloud.cmclink.doc.api.document.req.BaseCodeRpcReqDto;
import com.cmc.cloud.cmclink.doc.entity.BaseCodeDO;
import com.cmc.cloud.cmclink.doc.mapper.BaseCodeMapper;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 周贤
 * @date 2025/3/12
 */
@RestController
@Validated
public class BaseCodeApiImpl implements BaseCodeQueryApi {
    @Resource
    private BaseCodeMapper baseCodeMapper;

    @Override
    @UseResponsePropertyAdvice
    public CommonResult<List<BaseCodeRpcRepDto>> query(@Valid BaseCodeRpcReqDto dto) {
        LambdaQueryWrapper<BaseCodeDO> queryWrapper = new LambdaQueryWrapperX<BaseCodeDO>()
                .eqIfPresent(BaseCodeDO::getCode, dto.getCode())
                .eqIfPresent(BaseCodeDO::getCodeCategory, dto.getCodeCategory())
                .eqIfPresent(BaseCodeDO::getEnable, dto.getEnable())
                .orderByAsc(BaseCodeDO::getSortNo);
        List<BaseCodeDO> baseCodeDos = baseCodeMapper.selectList(queryWrapper);
        List<BaseCodeRpcRepDto> repDtoList = BeanUtil.copyToList(baseCodeDos, BaseCodeRpcRepDto.class);
        return CommonResult.success(repDtoList);
    }
}
